// pages/bianji/index.js
const {
    globalData
} = getApp()
import {
    legitimateIdCard,
    checkHealthCode
} from "../../api/home"
Page({

    /**
     * 页面的初始数据
     */
    data: {
        name: '',
        cardNum: '',
        phone: "",
        healthCode:"",
        type:"",
        qqqqqq: "1111",
        idzhengjian:''
    },
    //返回上一页
    onClickLeft() {
        globalData.currentIndex = -1
        globalData.currentItem = {}
        wx.navigateBack({
            delta: 1,
        })
    },
    onbindblues(e) {
        console.log(e);
    },
    //提交
    buybtn2() {
        const {
            name,
            cardNum,
            phone,
            id
        } = this.data
        const item = {
            name,
            cardNum,
            phone,
            id
        }
        const otherArr = globalData.idzhengjian.filter(item => item.id === this.data.id)
        console.log(otherArr,"我是提交8888");
        // console.log(this.data.cardNum);
        if (otherArr.filter(item => item.cardNum === this.data.cardNum).length > 0) {
            wx.showToast({
                title: '身份证已经存在',
                icon: 'none'
            })
            return
        }
        if(this.data.name == ''){
            wx.showToast({
              title: '姓名不能为空',
              icon:"none"
            })
        }else{
            legitimateIdCard({
                idCard: cardNum
            }).then((res) => {
                console.log(res);
                if (res.data.cardNumFlag == false) {
                    wx.showToast({
                        title: "身份证号码有误",
                        icon:'none'
                    })
                } else {
                    checkHealthCode({
                        cardNum: cardNum
                    }).then((res) => {
                        item.healthCode = res.data.healthCode
                        console.log(res);
                        this.setData({
                            type:2
                        })
                        globalData.currentItem = item
                        wx.navigateBack({
                            delta: 1,
                        })
                    })
                }
            })
        }
       

    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        // console.log(globalData.idzhengjian,"我是数据");
        // console.log(options, '我是编辑');
        const {
            name,
            cardNum,
            phone,
            healthCode,
            type,
            id,
        } = options
        this.setData({
            name:name == 'undefined'? '':name,
            cardNum:cardNum == 'undefined'? '':cardNum,
            phone :phone == 'undefined'? '':phone,
            type,
            idzhengjian:globalData.idzhengjian,
            id:id
        })
        const item = {
            name,
            cardNum,
            phone,
            healthCode
        }
        globalData.currentItem = item
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {
       
       
    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {
       if(this.data.type == 1){
        globalData.currentIndex = -1
        globalData.currentItem = {}
       }
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})